<?php
/**
 * Bootstrap.
 *  
 * @author Nguyen Hai Ha
 * @version 1.0
 */

error_reporting(E_ALL|E_STRICT);
date_default_timezone_set('Asia/Bangkok');

set_include_path('.' . PATH_SEPARATOR . './libs'
. PATH_SEPARATOR . './app/modules/models/'
. PATH_SEPARATOR . './app/utilities/'
. PATH_SEPARATOR . get_include_path());

require_once "Zend/Loader.php";
Zend_Loader::registerAutoload();

try{
	// Start session for application.
	Zend_Session::setOptions(array('save_path' => './app/tmp/sessions'));
	// Start session.
	Zend_Session::start();
} catch (Zend_Session_Exception $e) {
	// If have session error.
	// Auto reload page again.
	header("location:".$_SERVER["REQUEST_URI"]);
}

/** Loading application from session */
$application = new Zend_Session_Namespace(Constant::SESSION_NAMESPACE);

if (!isset($application->loggedUser)) {
    $application->loggedUser = null;
    $application->currentRole = Constant::GUEST;
}

/** Set cookie for user - use for remember language */
//$cookie_name = Constant::COOKIE_NAME;
//if (!isset($_COOKIE[$cookie_name])) {
//	setcookie($cookie_name, Constant::LANGUAGE_DEFAULT, time() + Constant::TIME_REMEMBER_USER, "/", false);	
//}
	
//$languageBaseUrl = './libs/Langs/' . Constant::LANGUAGE_DEFAULT;
//Zend_Registry::set(Constant::LANGUAGE_BASE_URL, $languageBaseUrl);

// Load all config.
$config = new Zend_Config_Xml('./app/configs/app_config.xml', 'live');
$config->setReadOnly();

// Setup database connection.
$dbAdapter = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
$dbAdapter->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Db_Table::setDefaultAdapter($dbAdapter);
Zend_Registry::set(CONSTANT::DB_ADAPTER, $dbAdapter);

$administrator_article_router_config = new Zend_Config_Ini('./app/configs/routers/administrator_article_router_config.ini', 'administrator_article');
$guest_article_router_config = new Zend_Config_Ini('./app/configs/routers/guest_news_router_config.ini', 'guest_article');
$router = new Zend_Controller_Router_Rewrite();
$router->addConfig($administrator_article_router_config, 'routes');
$router->addConfig($guest_article_router_config, 'routes');

// Setup controller.
$front = Zend_Controller_Front::getInstance();
$front->setBaseUrl('/gsm');
$front->setControllerDirectory(
		array(
			'guest' 		=> './app/modules/guest/controllers',
			'error' 		=> './app/modules/error/controllers',
			'member' 		=> './app/modules/member/controllers',
			'account' 		=> './app/modules/account/controllers',
			'administrator' => './app/modules/administrator/controllers'
		));

$front->setRouter($router);

/** Registering the Plugin object */  
$front->registerPlugin(new Hanh_Controller_Plugin_Acl(new Hanh_Controller_Acl_Resource(), $application->currentRole));

/** Registering the Action Helper object */  
Zend_Controller_Action_HelperBroker::addHelper(new Hanh_Controller_Action_Helper_Acl());

// Register plugin for auto error handler.
$plugin = new Zend_Controller_Plugin_ErrorHandler();
$plugin->setErrorHandlerModule('error')
       ->setErrorHandlerController('error')
       ->setErrorHandlerAction('error');
$front->registerPlugin($plugin);

try {
	// Run application!
	$front->dispatch();
} catch (Zend_Exception $e) {
	throw new Exception($e->getMessage().$e->getTraceAsString());
}